<?php

namespace app\admin\controller;
use app\admin\model\MenuModel;
use think\Db;

class Menu extends Base
{	
    /**
     * [index 菜单列表]
     * @return [type] [description]
     * @author
     */
    public function index()
    {
        extract(input());
        $map = [];
        if(isset($key)&&$key!="")
        {
            $map['title'] = ['like',"%" . $key . "%"];
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end=="")
        {
            $map['create_time'] = ['>= time',$start];
        }
        if(isset($end)&&$end!=""&&isset($start)&&$start=="")
        {
            $map['create_time'] = ['<= time',$end];
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end!="")
        {
            $map['create_time'] = ['between time',[$start,$end]];
        }
        $nav = new \org\Leftnav;
        $menu = new MenuModel();
        $Nowpage = 1;
        $limits = 1000;
        $count = Db::name('auth_rule')->where($map)->count();//计算总页面
        $admin_rule = $menu->getMenus($map, $Nowpage, $limits);
        $nav->init($admin_rule);
        $lists = $nav->get_tree(0);
        if(request()->isAjax ()){
            if(!empty($key) || !empty($start) || !empty($end)){
                return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$admin_rule]);
            }
//            $lists = $nav->rule($admin_rule);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }else{
//            $lists = $nav->rule($admin_rule);
            $this->assign ('admin_rule',$lists);
            return $this->fetch();
        }
    }

	
    /**
     * [add_rule 添加菜单]
     * @return [type] [description]
     * @author
     */
	public function add_rule()
    {
        if(request()->isPost()){
            extract(input());
            $pid = trim_explode (',',$pid);
            $title = trim_explode(',',$title);
            $name = trim_explode(',',$name);
            $css = trim_explode(',',$css);
            $sort = trim_explode(',',$sort);
            $menu = new MenuModel();
            $flag = $menu->insertMenu($pid,$title,$name,$css,$sort,$status);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('id');
        $nav = new \org\Leftnav;
        $menu = new MenuModel();
        $map = [];
        $admin_rule = $menu->getAllMenu($map);
        $nav->init($admin_rule);
        $lists = $nav->get_tree(0);
        $this->assign ([
            'admin_rule'=>$lists,
            'id'=>$id
        ]);
        return $this->fetch();
    }

    /**
     * [edit_rule 编辑菜单]
     * @return [type] [description]
     * @author
     */
    public function edit_rule()
    {
        $nav = new \org\Leftnav;
        $menu = new MenuModel();
        if(request()->isPost()){
            $param = input('');
            $param['name'] = strtolower($param['name']);
            $flag = $menu->editMenu($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('id');
        $map = [];
        $admin_rule = $menu->getAllMenu($map);
        $nav->init($admin_rule);
        $arr = $nav->get_tree(0);
//        $arr = $nav::rule($admin_rule);
        $this->assign ('admin_rule',$arr);
        $this->assign('menu',$menu->getOneMenu($id));
        return $this->fetch();
    }


    /**
     * [del_menu 删除菜单]
     * @return [type] [description]
     * @author
     */
    public function del_menu()
    {
        $id = input('param.id');
        $param[] = (int)$id;
        $son = Db::name('auth_rule')->where('pid',$id)->field('id')->select();
        if(!empty($son)){
            for($i=0;$i<count($son);$i++){
                if(!in_array($son[$i]['id'],$param)){
                    $param[] = $son[$i]['id'];
                }
                $ids = Db::name('auth_rule')->where('pid',$son[$i]['id'])->field('id')->select();
                if(!empty($ids)){
                    for($a=0;$a<count($ids);$a++){
                        if(!in_array($ids[$a]['id'],$param)){
                            $param[] = $ids[$a]['id'];
                        }
                    }
                    for($j=0;$j<count($ids);$j++){
                        $idss = Db::name('auth_rule')->where('pid',$ids[$j]['id'])->field('id')->select();
                        if(!empty($idss)){
                            for($b=0;$b<count($idss);$b++){
                                if(!in_array($idss[$b]['id'],$param)){
                                    $param[] = $idss[$b]['id'];
                                }
                            }
                        }
                    }
                }

            }
        }
        $param = array_unique($param);
        $menu = new MenuModel();
        $flag = $menu->delMenu($id,$param);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * [ruleorder 排序]
     * @return [type] [description]
     * @author
     */
    public function ruleorder()
    {
        extract(input());
        $menu = new MenuModel();
        $flag = $menu->ruleSort($id,$field,$value);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * editField 快捷编辑
     * @return \think\response\Json
     */
    public function editField(){
        extract(input());
        if($field == 'title'){
            $value = trim($value, chr(0xc2) . chr(0xa0));
            $value = str_replace ('│','',$value);
            $value = str_replace ('├─','',$value);
            $value = str_replace ('└─','',$value);
            $value = trim($value, chr(0xc2) . chr(0xa0));
        }
        $menu = new MenuModel();
        $flag = $menu->editField($id,$field,$value);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [rule_state 菜单状态]
     * @return [type] [description]
     * @author
     */
    public function rule_state()
    {
        extract(input());
        $menu = new MenuModel();
        $flag = $menu->ruleState($id,$num);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * batchDelMenu 批量删除菜单
     * @return \think\response\Json
     */
    public function batchDelMenu(){
        extract(input());
        if(empty($ids)){
            return json(['code'=>100,'msg'=>'请选择要删除的记录！']);
        }
        $ids = explode(',',$ids);
        $param = [];
        for($j=0;$j<count($ids);$j++){
            $param[] = (int)$ids[$j];
            $son = Db::name('auth_rule')->where('pid',$ids[$j])->field('id')->select();
            if(!empty($son)){
                for($i=0;$i<count($son);$i++){
                    if(!in_array($son[$i]['id'],$param)){
                        $param[] = $son[$i]['id'];
                    }
                    $num = Db::name('auth_rule')->where('pid',$son[$i]['id'])->field('id')->select();
                    if(!empty($num)){
                        for($a=0;$a<count($num);$a++){
                            if(!in_array($num[$a]['id'],$param)){
                                $param[] = $num[$a]['id'];
                            }
                        }
                        for($j=0;$j<count($num);$j++){
                            $idss = Db::name('auth_rule')->where('pid',$num[$j]['id'])->field('id')->select();
                            if(!empty($idss)){
                                for($b=0;$b<count($idss);$b++){
                                    if(!in_array($idss[$b]['id'],$param)){
                                        $param[] = $idss[$b]['id'];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $param = array_unique($param);
        $menu = new MenuModel();
        $flag = $menu->batchDelMenu($param);
        return json(['code' => $flag['code'],'data' => $flag['data'],'msg' => $flag['msg']]);
    }

}